package com.kaishengit.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtil {

	public static String getHttpContent(String url,String encoding) {
		HttpClient client = null;
		StringBuilder content = new StringBuilder();
		try {
			client = new DefaultHttpClient();
			
			HttpGet get = new HttpGet(url);
			
			HttpResponse response = client.execute(get);
			
			if(response.getStatusLine().getStatusCode() == 200) {
				
				InputStream stream = response.getEntity().getContent();
				
				BufferedReader reader = new BufferedReader(new InputStreamReader(stream,encoding));
				
				String str = null;
				
				
				while( (str = reader.readLine()) != null) {
					content.append(str);
				}
				
				reader.close();
				stream.close();
				
				System.out.println(content.toString());
				
			} else {
				System.out.println("Error!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return content.toString();
	}
}
